<?php

class ColorsController extends Controller
{
	public $layout='//layouts/column2';

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('*'),
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
    {
        session_start();
        $pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 15;
        $criteria=new CDbCriteria();
        $count=Colors::model()->count($criteria);

        $pages=new CPagination($count);
        $pages->pageSize=$pageSize;
        $pages->applyLimit($criteria);

        $sort=new CSort('Colors');
        $sort->defaultOrder='id';

        $dataProvider=new CActiveDataProvider('Colors', array(
            'criteria'=>$criteria,
            'pagination'=>$pages,
            'sort'=>$sort,
        ));

        if(isset($_POST['addColor'])){
            $_SESSION['colors'][]=$this->create();
            $this->redirect($this->createUrl('colors/index', array(
                'page'=>$pages->pageCount,
                'pageSize'=>$pageSize,
            )));
        }elseif(isset($_POST['saveColors']) && isset($_POST['Colors'])){
            $this->save();
        }

        $colors=$dataProvider->getData();

        if(isset($_SESSION['colors']) && $pages->currentPage==$pages->pageCount-1){
            foreach($_SESSION['colors'] as $color){
                $colors[]=$color;
            }
        }

        $this->render('index', array(
            'colors'=>$colors,
            'pages'=>$pages,
            'sort'=>$sort,
        ));
    }

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Colors the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
        $model=Colors::model()->findByPk($id);
        if($model===null){
            return false;
        }
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Colors $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='colors-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

    protected function save()
    {
        //echo '<pre>'; print_r($_POST['Colors']); exit;

        foreach($_POST['Colors'] as $i=>$color){
            $new_color=$this->loadModel($color['id']) ? $this->loadModel($color['id']) : new Colors;

            if(!$color['delete']){
                $new_color->attributes=$color;
                $new_color->save();
            }elseif($new_color->color){
                $new_color->delete();
            }
        }
        unset($_SESSION['colors']);
    }

    protected function create()
    {
        if(isset($_SESSION['colors'])){
            $last_color=end($_SESSION['colors']);
        }else{
            $criteria=new CDbCriteria(array(
                'order'=>'t.id DESC',
                'limit'=>1,
            ));
            $last_color=Colors::model()->find($criteria);
        }

        $new_color=new Colors;
        $new_color->id=$last_color ? $last_color->id+1 : 1;
        $new_color->color='Цвет '.($new_color->id);
        $new_color->r=0;
        $new_color->g=0;
        $new_color->b=0;

        return $new_color;
    }
}
